function findpath (arr) {
  let len = arr.length, res = new Array(len).fill(Infinity), step = 0;
  res[0] = 0;
  while (res[len - 1] == Infinity) {
    for (let i = 0; i < len; i++) {
      if (res[i] == step) {
        fillres(arr, i, res, step)
      }
      step++
    }
  }
  return res[len - 1]
}
function fillres (arr, index, res, step) {
  if (index > 0) {
    res[index - 1] = Math.min(res[index - 1], step + 1)
  }
  if (index < arr.length - 1) {
    res[index + 1] = Math.min(res[index + 1], step + 1)
  }
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == arr[index] && i !== index) {
      res[i] = Math.min(res[i], step + 1)
    }
  }
}
let arr = [6, 5, 14, 24, 5, 100, 1];
console.log(findpath(arr))
